/**
*  \file    systime.c
*
*  \brief   Simple Tick Counter
*
*  \author  Michael J. Hohmann <Michael.Hohmann@linde-mh.de>
*
*
*  The systime module provide an simple tick counter.
*
*/

/*===============================================[ private includes  ]================================================*/
#define SYSTIME_LINKAGE
#include "../systime/systime.h"

/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/
static systime_t volatile systime_ticks;

/*===============================================[ public variables  ]================================================*/

/*===============================================[ private functions ]================================================*/

/*====================================================================================================================*/

/** Initialization.
*
*  Initialize the systime module.
*
*  \return always RC_SUCCESS
*/
RC systime_initialize( void )
{
   systime_ticks=0;
   return RC_SUCCESS;
}

/** Increment system ticks.
*
*  The systime_tick() increment the internal system tick counter by one.
*  The isochronous call of this function is expected.
*/
void systime_tick( void )
{
   systime_ticks++;
}

/** Get ticks.
*
*  \return Number of ticks since system start.
*
*  \attention
*/
systime_t systime_get(void)
{
   systime_t t1 ;
   systime_t t2 ;

   do
   {
      t1 =  systime_ticks;
      t2 =  systime_ticks;
   }
   while(t1!=t2);

   return t1;
}


/** Period of time is elapsed.
*
*  If the constant SYSTIME_NO_TIMEOUT will passed as period the result is always FALSE.
*  If zero will be passed as period the result will be always TRUE.
*
*  \return TRUE if more than the passed period of time is elapsed.
*
*  \sa SYSTIME_NO_TIMEOUT
*/
BO systime_timeout(systime_t start_time, systime_t period )
{
   switch(period)
   {
      case 0                     : return TRUE;
      case SYSTIME_NO_TIMEOUT    : return FALSE;
      default                    : return ( systime_get() - start_time ) >= period;
   }
}

/** Wait a period of time
*
*  The function systime_delay() will return after the passed period of time.
*  It has a jitter of 1 tick so you may want to add one tick to the period.
*  The value 0 passed to the function will return immediately.
*  The value SYSTIME_NO_TIMEOUT passed to the function will NEVER EVER return !
*
*  \param period Time period to delay.
*/
void systime_delay( systime_t period )
{
   systime_t entry_time = systime_get();

   while( ! systime_timeout(entry_time, period) )
   {
      /*
         I wasted time, and now doth time waste me;
         For now hath time made me his numbering clock:
         My thoughts are minutes; and with sighs they jar
         Their watches on unto mine eyes, the outward watch,
         Whereto my finger, like a dial's point,
         Is pointing still, in cleansing them from tears.
         Now sir, the sound that tells what hour it is
         Are clamorous groans, which strike upon my heart,
         Which is the bell: so sighs and tears and groans
         Show minutes, times, and hours.
      */
    }
}
/***********************************************************************************************************************
*                             Copyright 2007 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
